package pl.edu.mimuw.irs.core.model.design.net;

import java.util.LinkedHashMap;
import java.util.Map;

import pl.edu.mimuw.irs.core.model.design.exception.InternalDesignException;
import pl.edu.mimuw.irs.core.model.design.exception.InternalDesignException.DesignCause;

public class IRSPlaceCopier extends AbstractIRSNodeCopier {

	// *** Attributes

	private IRSPlace place;
	private int tokens;
	public IRSPlace getPlace() {
		return place;
	}
	public int getTokens() {
		return tokens;
	}
	
	// *** Constructors
	
	public IRSPlaceCopier(IRSPlace place) {
		super(place);
		this.place = place;
		this.tokens = place.getTokens();
	}
	
	// *** Methods
	
	protected IRSNode createPlainCopy(Map<IRSNode, IRSNode> clonesMap) {
		IRSPlace clone = new IRSPlace(getIdManager(), 
				getIdManager().getPrefixedNewId(getId()), getNodeType(), getPosition().copy(), getTokens());
		return clone;
	}
	
	@Override
	protected void copyAttributesTo(IRSNode target) {
		if (!(target instanceof IRSPlace)) 
			throw new InternalDesignException(DesignCause.INCOMPATIBLE_COPY_NODES, getNode(), target);
		IRSPlace targetPlace = (IRSPlace) target;
		targetPlace.setTokens(getTokens());
	}
	
	public IRSRequirement copyAsRequirement() {
		return copyAsRequirement(CLONE_INCOMING_ARCS | CLONE_OUTGOING_ARCS);
	}
	
	public IRSRequirement copyAsRequirement(int options) {
		Map<IRSNode, IRSNode> clonedMap = new LinkedHashMap<IRSNode, IRSNode>();
		IRSRequirement clone = copyAsRequirement(clonedMap);
		if ((CLONE_INCOMING_ARCS & options) != 0) cloneIncomingArcs(getNode(), clone, clonedMap);
		if ((CLONE_OUTGOING_ARCS & options) != 0) cloneOutgoingArcs(getNode(), clone, clonedMap);
		return clone;
	}
	
	protected IRSRequirement copyAsRequirement(Map<IRSNode, IRSNode> clonesMap) {
		IRSRequirement clone = new IRSRequirement(getIdManager(),
				getIdManager().getPrefixedNewId(getId()), getNodeType(), getPosition().copy(), getTokens());
		copyContents(getNode(), clone, clonesMap);
		if (getNet() != null) getNet().addPlace(clone);
		return clone;
	}
}
